{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-15T12:46:41.545399Z",
     "start_time": "2025-02-15T12:46:40.953873Z"
    }
   },
   "source": [
    "import gradio as gr\n",
    "import requests\n",
    "import json\n",
    "\n",
    "# 替换为你的 DeepSeek API 密钥\n",
    "API_KEY = 'sk-xgxbqajfxbsfptqumdbsrtiakcisgochhzyltatkcqtarlvq'\n",
    "API_URL = 'https://api.deepseek.com/v1/chat/completions'\n",
    "\n",
    "def chat_with_deepseek(prompt):\n",
    "    headers = {\n",
    "        'Authorization': f'Bearer {API_KEY}',\n",
    "        'Content-Type': 'application/json'\n",
    "    }\n",
    "\n",
    "    data = {\n",
    "        \"model\": \"deepseek-chat\",  # 替换为你想使用的模型\n",
    "        \"messages\": [\n",
    "            {\"role\": \"user\", \"content\": prompt}\n",
    "        ]\n",
    "    }\n",
    "\n",
    "    response = requests.post(API_URL, headers=headers, data=json.dumps(data))\n",
    "    if response.status_code == 200:\n",
    "        return response.json()['choices'][0]['message']['content']\n",
    "    else:\n",
    "        return f\"Error: {response.status_code}, {response.text}\"\n",
    "\n",
    "# 创建 Gradio 界面\n",
    "def gradio_chat(prompt):\n",
    "    return chat_with_deepseek(prompt)\n",
    "\n",
    "iface = gr.Interface(\n",
    "    fn=gradio_chat,\n",
    "    inputs=\"text\",\n",
    "    outputs=\"text\",\n",
    "    title=\"DeepSeek Chat\",\n",
    "    description=\"与 DeepSeek 进行对话\"\n",
    ")\n",
    "\n",
    "iface.launch()"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "* Running on local URL:  http://127.0.0.1:7861\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ],
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "12dabf8e9da4df3a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
